<?php
namespace Common\Model;

/**
 * 省份资源表
 * @modify trainyao 2015-12-31 创建
 * @index province(`country_id`)
 */

Class ProvinceModel extends \Common\Model\BaseModel
{
    protected $trueTableName = 'bk_province';
    
    /**
     * 获取省份数据
     * @modify trainyao 2015-12-31  创建
     * @param  integer   $id 省份id
     * @return array
     */
    public function getInfo($id = null) {
        $option = array();
        if (!empty($id)) {
            $option['id'] = $id;
        }
        $option['isdelete'] = NOT_DELETE;
        
        $fields = 'id,country_id,name';
        $data = array();
        
        if (!empty($id)) {
            $data = $this->where($option)->field($fields)->find();
        }
        $data = $this->where($option)->field($fields)->select();
        
        if (empty($data)) {
            return array();
        }
        return $data;
    }
    
    /**
     * 根据国家取省份
     * @modify trainyao 2015-12-31 创建
     * @param  integer   $countryId 国家id
     * @return array
     */
    public function getInfoByCountry($countryId) {
        $option = array('country_id' => $countryId, 'isdelete' => NOT_DELETE);
        $fields = 'id,country_id,name';
        
        $data = $this->where($option)->field($fields)->select();
        
        if (empty($data)) {
            return array();
        }
        return $data;
    }
    
    /**
     * 增加省份
     * @modify trainyao 2015-12-31 创建
     * @param  string   $countryId      国家id
     * @param  string   $name      省份名
     * @return boolean 执行结果
     */
    public function addInfo($countryId, $name) {
        $data = array('country_id' => $countryId, 'name' => $name);
        return $this->add($data) === false ? false : true;
    }
}
